describe('数据库验证测试 - 确认真实INSERT操作', () => {
  let testData

  before(() => {
    cy.fixture('test-data').then((data) => {
      testData = data
    })
  })

  describe('📊 数据库INSERT验证', () => {
    it('应该验证Oracle数据库中的实际INSERT操作', () => {
      const timestamp = Date.now()
      const uniqueOrderNo = `DBTEST${timestamp}`
      
      // 准备唯一的测试数据避免冲突
      const operatorConfirmRequest = {
        orderNo: uniqueOrderNo,
        orderBranchNo: "01",
        mounterId: "M05",
        smtSide: "TOP",
        operatorId: testData.operators.valid.operatorId,
        lineId: "LINE01"
      }

      cy.log(`🗄️ 开始数据库INSERT验证测试 - 订单号: ${uniqueOrderNo}`)

      // 1. 先保存作业员确认记录
      cy.apiRequest('POST', '/api/smt/saveOperatorConfirm', operatorConfirmRequest)
        .then((response) => {
          cy.log(`作业员确认响应: ${response.body.code} - ${response.body.message}`)
          
          if (response.body.code === testData.responseCodes.success) {
            cy.log('✅ 作业员确认成功 - 数据已INSERT到T_SMT_PROCESS表')
            const processId = response.body.data.processId
            cy.log(`流程ID: ${processId}`)
            
            // 2. 保存扫描记录
            const scanRecordRequest = {
              orderInfo: operatorConfirmRequest,
              successRecords: [
                {
                  partId: `DBTEST_PART_${timestamp}`,
                  rollNo: `DBTEST_ROLL_${timestamp}`,
                  lotNo: `DBTEST_LOT_${timestamp}`,
                  scanTime: new Date().toISOString(),
                  scanStatus: 'SUCCESS'
                }
              ],
              failureRecords: []
            }

            cy.apiRequest('POST', '/api/smt/saveScanRecord', scanRecordRequest)
              .then((scanResponse) => {
                cy.log(`扫描记录保存响应: ${scanResponse.body.code} - ${scanResponse.body.message}`)
                
                if (scanResponse.body.code === testData.responseCodes.success) {
                  cy.log('🎉 扫描记录保存成功!')
                  cy.log('💾 数据已INSERT到以下Oracle表:')
                  cy.log('   - T_SMT_PROCESS (作业员确认记录)')
                  cy.log('   - T_SMT_ROLL (成功扫描记录)')
                  cy.log(`📊 成功记录数: ${scanResponse.body.data.successCount}`)
                  cy.log(`📊 失败记录数: ${scanResponse.body.data.failureCount}`)
                  cy.log(`📊 总记录数: ${scanResponse.body.data.totalCount}`)
                } else {
                  cy.log(`❌ 扫描记录保存失败: ${scanResponse.body.code}`)
                  cy.log('可能原因: 订单或流程数据不存在')
                }
              })
          } else if (response.body.code === testData.responseCodes.orderNotFound) {
            cy.log('📋 订单不存在 - 需要先在数据库中创建测试订单')
          } else {
            cy.log(`❌ 作业员确认失败: ${response.body.code} - ${response.body.message}`)
          }
        })
    })

    it('应该测试真实的数据库约束和验证', () => {
      // 测试数据库约束：提交无效的分支号
      const invalidRequest = {
        orderNo: testData.orders.valid.orderNo,
        orderBranchNo: "INVALID", // 应该是数字
        mounterId: testData.orders.valid.machineId,
        smtSide: "TOP",
        operatorId: testData.operators.valid.operatorId,
        lineId: "LINE01"
      }

      cy.apiRequest('POST', '/api/smt/saveOperatorConfirm', invalidRequest)
        .then((response) => {
          expect(response.status).to.equal(200)
          expect(response.body.code).to.equal(testData.responseCodes.badRequest)
          cy.log(`✅ 数据验证正常工作: ${response.body.message}`)
        })
    })
  })
})